<script lang="ts" setup>
import { useMemberStore } from '@/stores'
import { onShow } from '@dcloudio/uni-app'
import { getMyCouponsApi } from '@/services/home'
import { ref } from 'vue'

// 子调父组件
const emit = defineEmits<{
  (event: 'open'): void
  (e: 'unfinishedOpen'): void
}>()
const memberStore = useMemberStore()
const _value = ref()
onShow(() => {
  if (!memberStore.token) return
  getMyCoupons()
})
/**获取待使用状态的优惠券长度 */
const getMyCoupons = async () => {
  const res = await getMyCouponsApi({
    status: 1,
    clientId: memberStore.userInfo?.id,
    pageNum: 1,
    pageSize: 99999,
  })
  _value.value = res.data?.rows?.length
}

defineExpose({
  getMyCoupons,
})
// console.log(props.loginFlag)
</script>
<template>
  <view class="PersonageLogin" v-if="memberStore.token">
    <view class="logo_left">
      <image
        class="logo_image"
        :src="memberStore.userInfo?.headImg || `/static/images/logo.png`"
        mode="scaleToFill"
      />
      <view class="logo_text">
        <text>{{ memberStore.userInfo?.nickName }}</text>
        <text class="text_png">欢迎加入美味健康之旅</text>
      </view>
    </view>
    <!-- 跳转优惠券界面 /pagesMy/userCoupon/userCoupon -->
    <navigator url="/pagesMy/userCoupon/userCoupon" hover-class="none" class="iconfont icon_Frame"
      ><view class="CouponsLength">{{ _value || 0 }}张</view></navigator
    >
  </view>
  <view class="Personage" v-else>
    <view class="logo_left">
      <image class="logo_image" src="/static/images/logo.png" mode="scaleToFill" />
      <view class="logo_text">
        <image class="text_png" src="/static/images/logo_text.png" mode="aspectFit" />
        <view>马上登录享更多优惠权益</view></view
      >
    </view>
    <view class="logo_right" @tap.stop="emit('open')">立即登录</view>
  </view>
</template>

<style lang="scss">
.Personage {
  display: inline-flex;
  padding: 60rpx 40rpx;
  width: 100%;
  height: 188rpx;
  justify-content: space-between;
  align-items: center;
  background-color: #fff;
  box-sizing: border-box;
  .logo_left {
    display: flex;
    align-items: center;
    justify-content: space-between;
    .logo_image {
      border-radius: 50%;
      width: 80rpx;
      height: 80rpx;
      margin-right: 16rpx;
    }
    .logo_text {
      width: 350rpx;
      font-size: 22rpx;
      color: rgba(133, 133, 133, 0.5);
      .text_png {
        width: 132rpx;
        height: 46rpx;
      }
    }
  }
  .logo_right {
    display: flex;
    justify-content: center;
    width: 148rpx;
    height: 52rpx;
    line-height: 52rpx;
    font-family: 'PingFang SC';
    font-size: 24rpx;
    color: #000;
    background-color: $uni-color-motif;
    border-radius: 68rpx;
    z-index: 9;
  }
}
.PersonageLogin {
  display: inline-flex;
  padding: 60rpx 40rpx;
  width: 100%;
  height: 188rpx;
  justify-content: space-between;
  align-items: center;
  background-color: #fff;
  .logo_left {
    display: flex;
    align-items: center;
    .logo_image {
      border-radius: 50%;
      width: 80rpx;
      height: 80rpx;
      margin-right: 16rpx;
    }
    .logo_text {
      font-size: 28rpx;
      display: flex;
      flex-direction: column;
      .text_png {
        font-size: 22rpx;
        color: rgba(133, 133, 133, 0.5);
      }
    }
  }
  .iconfont {
    min-width: 48rpx;
    height: 48rpx;
    font-size: 48rpx;
    color: $uni-color-motif;
    display: flex;
    align-items: center;
    .CouponsLength {
      margin-left: 16rpx;
      display: flex;
      align-items: center;
      font-size: 26rpx;
      color: #000;
    }
  }
}
</style>
